package com.city.ajax.pub;

import com.city.domain.city.CityEvent;
import com.city.domain.city.CityHouseinfo;
import com.city.domain.pub.PubHouseinfo;
import com.city.domain.supervise.*;
import com.city.domain.pub.PubUser;
import com.city.helper.AuthHelper;
import com.city.service.pub.PubHouseinfoService;
import com.city.service.pub.PubUserService;
import com.city.service.pub.SaveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * @deprecation: 公共存储接口
 * @dmail: 398521613@qq.com
 * @author: L-H
 * @date: 2018-07-20 16:32
 */

@RestController
@RequestMapping("/ajax/save")
public class PubSaveAjax {

    @Autowired
    public SaveService saveService;

    @Autowired
    private PubUserService userService;

    @Autowired
    private PubHouseinfoService houseinfoService;

    @Autowired
    private AuthHelper authHelper;



    /**
     * @param cityEvent 案件
     * @deprecation: 保存案件，tim值很重要，不可重复
     * @dmail: 398521613@qq.com
     * @author: L-H
     * @date: 2018-07-20 16:32
     */
    @PostMapping("/event")
    @Transactional
    @ResponseBody
    public Map<String, String> saveEvent(@RequestBody CityEvent cityEvent,
                                         HttpServletResponse response,
                                         HttpSession session) {
        Map<String, String> map = new HashMap<String, String>();

        System.out.println(cityEvent);

        //提取信息
        String loginTel = (String) session.getAttribute("loginTel");
        loginTel = "15708197406";

//        if(loginTel == null) response.sendRedirect(system != null ? "/login.html?system=" + system : "/");
        //如果没有登陆
        if (loginTel == null) {
            map.put("res", "-1");
            map.put("code", "未登录,请登录");
            return map;
        }
        PubUser user = userService.getUserByTell(loginTel);
        if (user == null) {
            map.put("res", "-1");
            map.put("code", "未登录,请登录");
            return map;
        }
        //更新内容
//        copnote.setReportId(user.getId());
        cityEvent.setReportHumanName(user.getName());
        //保存基本信息
        saveService.save(cityEvent);
        map.put("res", "1");
        map.put("code", "上报信息已录入，请等待回复！");
        return map;
    }

    /**
     * @deprecation: 居民房屋信息
     * @dmail: 398521613@qq.com
     * @author: L-H
     * @date: 2018-07-20 16:32
     */
    @PostMapping("/houseinfo")
    @Transactional
    @ResponseBody
    public Map<String, String> savePubHouseinfo(@RequestBody PubHouseinfo pubHouseinfo,
                                                HttpServletResponse response,
                                                HttpSession session) {
        Map<String, String> map = new HashMap<String, String>();

        System.out.println(pubHouseinfo);



        PubUser user = userService.getUserByIdCard(pubHouseinfo.getIdCard());
        if (user == null) {
            map.put("code", "业主身份证件不存在！");
            return map;
        }
        //更新内容
        pubHouseinfo.setUserId(user.getId());
//        user = userService.getUserByIdCard(pubHouseinfo.getLandIdCard());
//        if (user == null) {
//            map.put("code", "户主信息有误,请确认后重试");
//            return map;
//        }
        //更新内容
        pubHouseinfo.setLandUid(user.getId());
        pubHouseinfo.setStatus(0);
        pubHouseinfo.setUploadName(userService.findById(authHelper.thisUserId()).getName());
        //保存基本信息
        houseinfoService.save(pubHouseinfo);
        map.put("res", "1");
        map.put("code", "信息已录入，请等待回复！");
        return map;
    }

    /**
     * @param pubHouseinfo 居民房屋
     * @deprecation: 居民房屋信息审核验证
     * @dmail: 398521613@qq.com
     * @author: L-H
     * @date: 2018-07-20 16:32
     */
    @PostMapping("/houseinfo-examine")
    @Transactional
    @ResponseBody
    public Map<String, String> savePubHouseinfoExamine(@RequestBody PubHouseinfo pubHouseinfo,
                                                       HttpServletResponse response,
                                                       HttpSession session) {
        Map<String, String> map = new HashMap<String, String>();

        System.out.println(pubHouseinfo);

        String loginTel = (String) session.getAttribute("loginTel");
        loginTel = "15708197406";

        //如果没有登陆
        if (loginTel == null) {
            map.put("res", "-1");
            map.put("code", "未登录,请登录");
            return map;
        }
        //更新内容
        pubHouseinfo.setStatus(1);
        //保存基本信息
        saveService.save(pubHouseinfo);
        map.put("res", "1");
        map.put("code", "审核完毕");
        return map;
    }

    /**
     * @param id Integer 居民房屋id
     * @Author: 李顺时
     * @Description: 实有房屋审核
     * @Date: 14:48 2018/7/29
     */
    @PostMapping("/houseinfo-status")
    @Transactional
    @ResponseBody
    public Map<String, String> savePubHouseinfoExamine(@Param("id") Integer id) {
        Map<String, String> map = new HashMap<String, String>();

        //更新内容
        PubHouseinfo pubHouseinfo = houseinfoService.getHouseinfo(id);
        pubHouseinfo.setStatus(1);

        //保存基本信息
        saveService.save(pubHouseinfo);
        map.put("res", "1");
        map.put("code", "审核完毕");
        return map;
    }

    /**
     * @Author: 李顺时
     * @Description: 房屋审核-拒绝
     * @Param: id Integer 居民房屋id
     * @Date: 9:22 2018/7/30
     */
    @PostMapping("/houseinfo-deny")
    @Transactional
    @ResponseBody
    public Map<String, Object> savePubHouseinfoDeny(@Param("id") Integer id) {
        Map<String, Object> map = new HashMap<String, Object>();

        //更新内容
        PubHouseinfo pubHouseinfo = houseinfoService.getHouseinfo(id);
        pubHouseinfo.setStatus(-1);

        //保存基本信息
        saveService.save(pubHouseinfo);
        map.put("data", houseinfoService.getHouseinfo(id));
        map.put("res", "1");
        map.put("code", "审核完毕");
        return map;
    }


    /**
     * @param cityHouseinfo 出租屋信息
     * @deprecation: 新增出租屋
     * @dmail: 398521613@qq.com
     * @author: L-H
     * @date: 2018-07-20 16:32
     */
    @PostMapping("/cityhouseinfo")
    @Transactional
    @ResponseBody
    public Map<String, String> saveCityHouseInfo(@RequestBody CityHouseinfo cityHouseinfo,
                                                 HttpServletResponse response,
                                                 HttpSession session) {
        Map<String, String> map = new HashMap<String, String>();

        System.out.println(cityHouseinfo);

        PubUser user = userService.getUserByTell(cityHouseinfo.getTellphone());

        if (user != null) {
            cityHouseinfo.setUserId(user.getId());
        }
        //保存基本信息
        BigDecimal b = new BigDecimal(0);//设置经纬度
        cityHouseinfo.setLat(b);
        cityHouseinfo.setLng(b);
        saveService.save(cityHouseinfo);
        map.put("res", "1");
        map.put("code", "完成！");
        return map;
    }

    /**
     * @param pubKeyPerson 重点人信息
     * @deprecation: 新增新增重点人
     * @dmail: 398521613@qq.com
     * @author: L-H
     * @date: 2018-07-20 16:32
     */
    @PostMapping("/keyperson")
    @Transactional
    @ResponseBody
    public Map<String, String> saveKeyPersonInfo(@RequestBody PubKeyPerson pubKeyPerson,
                                                 HttpServletResponse response,
                                                 HttpSession session) {
        Map<String, String> map = new HashMap<String, String>();

        PubUser user = userService.getUserByTellAndName(pubKeyPerson.getName(), pubKeyPerson.getTellphone());

/*        if(user != null){
            pubKeyPerson.setUserId(user.getId());
        }*/
        if (pubKeyPerson.getId() == null || pubKeyPerson.getId() <= 0) {
            pubKeyPerson.setStatus(1);
        }
        //保存基本信息
        saveService.save(pubKeyPerson);
        map.put("res", "1");
        map.put("code", "完成！");
        return map;
    }


    /**
     * @param
     * @deprecation: 新增重点地点
     * @author: 肖丽娜
     * @date: 2018-08-15 16:32
     */


    @PostMapping("/keylocation")
    @Transactional
    @ResponseBody
    public Map<String, String> saveKeyLocationInfo(@RequestBody ImpLocation impLocation,
                                                   HttpServletResponse response,
                                                   HttpSession session) {
        Map<String, String> map = new HashMap<String, String>();
        System.out.println("--------------------------");
        System.out.println(impLocation);
        PubUser user = userService.getUserByTellAndName(impLocation.getLoname(), impLocation.getManame());
        if (impLocation.getId() == null || impLocation.getId() <= 0) {
            impLocation.setId(0);
        }
        //保存基本信息

        saveService.save(impLocation);
        map.put("res", "1");
        map.put("code", "完成！");
        return map;

    }


    /**
     * @param
     * @deprecation: 新增重点事件分类管理
     * @author: 肖丽娜
     * @date: 2018-08-15 16:32
     */

    @PostMapping("/keycategory")
    @Transactional
    @ResponseBody
    public Map<String, String> saveKeyCategoryInfo(@RequestBody ImpCategory impCategory,
                                                   HttpServletResponse response,
                                                   HttpSession session) {
        Map<String, String> map = new HashMap<String, String>();
        PubUser user = userService.getUserByTellAndName(impCategory.getEvent_name(), impCategory.getTellphone());
        if (impCategory.getId() == null || impCategory.getId() <= 0) {
            impCategory.setId(0);
        }
        //保存基本信息
        saveService.save(impCategory);
        map.put("res", "1");
        map.put("code", "完成！");
        return map;
    }

    /**
     * @param
     * @deprecation: 新增重点物件管理
     * @author: 肖丽娜
     * @date: 2018-08-15 16:32
     */
    @PostMapping("/keything")
    @Transactional
    @ResponseBody
    public Map<String, String> saveKeyThingInfo(@RequestBody ImpThing impThing,
                                                   HttpServletResponse response,
                                                   HttpSession session) {
        Map<String, String> map = new HashMap<String, String> ();
        PubUser user = userService.getUserByTellAndName(impThing.getThing_name(), impThing.getTellphone());
        if (impThing.getId() == null || impThing.getId() <= 0) {
            impThing.setId(0);
        }
        //保存基本信息
        saveService.save(impThing);
        map.put("res", "1");
        map.put("code", "完成！");
        return map;

    }

    /**
     * @param
     * @deprecation: 新增重点组织管理
     * @author: 肖丽娜
     * @date: 2018-08-15 16:32
     */
    @PostMapping("/keyorganization")
    @Transactional
    @ResponseBody
    public Map<String, String> saveKeyOrganizationInfo(@RequestBody ImpOrganization impOrganization,
                                                   HttpServletResponse response,
                                                   HttpSession session) {
        Map<String, String> map = new HashMap<String, String> ();
        PubUser user = userService.getUserByTellAndName(impOrganization.getOrganize_name(), impOrganization.getTellphone());
        if (impOrganization.getId() == null || impOrganization.getId() <= 0) {
            impOrganization.setId(0);
        }
        //保存基本信息
        saveService.save(impOrganization);
        map.put("res", "1");
        map.put("code", "完成！");
        return map;

    }





}